<?php


namespace app\api\controller;
use app\api\model\Works as WorksModel;
use app\api\model\Banner as BannerModel;
use app\api\model\WorksSeeLog as WorksSeeLogModel;
use app\api\model\BingeWatching as BingeWatchingModel;
use app\api\model\WorksChapter as WorksChapterModel;

class Index extends Base
{

    /**
     * 构造方法
     * Oauth constructor.
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * 首页轮播图
     * date 2021/3/8 10:34
     */
    public function banner() {
        $list = BannerModel::where(['status'=>1])->select();
        foreach ($list as &$item){
            $item['image'] = 'http://www.works.com/'.str_ireplace('../../','',$item['image']);
        }
        json_response(1,'',$list);
    }

    /**
     * 首页优选好剧
     * date 2021/3/8 10:34
     */
    public function works_index() {
        $list = WorksModel::field('id,title,cover_img')->where(['status'=>1])->select();
        foreach ($list as &$item){
            $item['cover_img'] = 'http://www.works.com/'.str_ireplace('../../','',$item['cover_img']);
        }
        json_response(1,'',$list);
    }

    /**
     * 追剧-最近观看
     * date 2021/3/8 10:34
     */
    public function works_see_log() {
        $token = input('token');
        $uid = db('user')->where('token',$token)->value('id');
        if (!$uid) json_response(0,'参数错误');
        $where = [
            ['wsl.is_del','=',0],
            ['wsl.uid','=',$uid]
        ];
        $list = WorksSeeLogModel::alias('wsl')
            ->field('w.id,w.title,w.cover_img')
            ->join('works_chapter wc','wc.id=wsl.wc_id')
            ->join('works w','w.id=wc.w_id')
            ->where($where)
            ->select();
        foreach ($list as &$item){
            $item['cover_img'] = 'http://www.works.com/'.str_ireplace('../../','',$item['cover_img']);
        }
        json_response(1,'',$list);
    }

    /**
     * 追剧-我的追剧
     * date 2021/3/8 10:34
     */
    public function binge_watching() {
        $token = input('token');
        $uid = db('user')->where('token',$token)->value('id');
        if (!$uid) json_response(0,'参数错误');
        $where = [
            ['bw.is_del','=',0],
            ['bw.uid','=',$uid]
        ];
        $list = BingeWatchingModel::alias('bw')
            ->field('w.id,w.title,w.cover_img')
            ->join('works_chapter wc','wc.id=bw.wc_id')
            ->join('works w','w.id=wc.w_id')
            ->where($where)
            ->select();
        foreach ($list as &$item){
            $item['cover_img'] = 'http://www.works.com/'.str_ireplace('../../','',$item['cover_img']);
        }
        json_response(1,'',$list);
    }

    /**
     * 视频详情
     * date 2021/3/8 10:34
     */
    public function video_list(){
        $token = input('token');
        $uid = db('user')->where('token',$token)->value('id');
        if (!$uid) json_response(0,'参数错误');
        $w_id = input('w_id');
        $where = [
            ['is_del','=',0],
            ['w_id','=',$w_id],
        ];
        $list = WorksChapterModel::where($where)->select();
        foreach ($list as &$item){
            $item['video_path'] = $item['video_url'];
            //是否点赞（喜欢）
            $is_dianzan = db('works_like')->where([
                ['uid','=',$uid],
                ['wc_id','=',$item['id']]
            ])->value('id');
            if ($is_dianzan){
                $item['is_dianzan'] = true;
            }else{
                $item['is_dianzan'] = false;
            }
            //是否追剧
            $is_binge_watching = db('binge_watching')->where([
                ['uid','=',$uid],
                ['wc_id','=',$item['id']]
            ])->value('id');
            if ($is_binge_watching){
                $item['is_binge_watching'] = true;
            }else{
                $item['is_binge_watching'] = false;
            }
        }
        json_response(1,'',$list);
    }

}